package com.iSnail.ms.gather.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;

import com.iSnail.ms.gather.Context;
import com.iSnail.ms.util.Const;
import com.iSnail.ms.util.I18N;

public class Temperature {

	DefaultValueDataset localDefaultValueDataset = null;
	JCheckBox used = new JCheckBox(I18N.getString("used"));
	int num = 0;

	public Temperature(int num) {
		this.num = num;
	}

	private JFreeChart createChart(String name) {
		localDefaultValueDataset = new DefaultValueDataset(37.200000000000003D);
		ThermometerPlot localThermometerPlot = new ThermometerPlot(localDefaultValueDataset);
		localThermometerPlot.setRange(-500, 3000);
		JFreeChart localJFreeChart = new JFreeChart(name, localThermometerPlot);
		localJFreeChart.setBackgroundPaint(Color.white);
		return localJFreeChart;
	}

	public JPanel createDemoPanel(String name) {
		JFreeChart localJFreeChart = createChart(name);
		new Thread(){
			public void run() {
				showView();
			}
		}.start();
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.add(new ChartPanel(localJFreeChart),
				new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(used,
				new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		return panel;
	}

	private void showView(){
		try {
			while (true){
				if(used.isSelected() && 
						Context.presschanel != null ){
					localDefaultValueDataset.setValue(Context.presschanel[num]);
				}
				Thread.sleep(1000);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}
